/*
 * TemplatePattern.cpp
 *
 *  Created on: 18/nov/2010
 *      Author: computer
 */

#ifndef TEMPLATEPATTERN_CPP
#define TEMPLATEPATTERN_CPP

#include "Pattern.h"
#include "Expression.h"
#include "Context.h"

namespace PatternRecognition {

template<typename T> SinglePattern<T>::SinglePattern(T const& _prs) : prs(_prs) { }

template<typename T> void SinglePattern<T>::set(const AutoPointer<TreeExpression::Expression>& expr, CopyPointer<Context>& context) const {
	return prs.set(expr, context);
}

template<typename T> int SinglePattern<T>::match(const AutoPointer<TreeExpression::Expression>& expr) {
	return prs.match(expr);
}

template<typename T> void SinglePattern<T>::setName(const std::string& _name) {
	prs.setName(_name);
}

template<typename T> std::string SinglePattern<T>::getName() const {
	return prs.getName();
}

template<typename T> std::string SinglePattern<T>::getHash() const {
	return prs.getHash();
}

template<typename T> std::string SinglePattern<T>::toString() const {
	return prs.toString();
}

template<typename T> void Pattern<T>::set(const AutoPointer<TreeExpression::Expression>& expr, CopyPointer<Context>& context) const {
	return pattern->set(expr, context);
}

template<typename T> int Pattern<T>::match(const AutoPointer<TreeExpression::Expression>& expr) {
	return pattern->match(expr);
}

template<typename T> void Pattern<T>::setName(const std::string& _name) {
	pattern->setName(_name);
}

template<typename T> std::string Pattern<T>::getName() const {
	return pattern->getName();
}

template<typename T> std::string Pattern<T>::getHash() const {
	return pattern->getHash();
}

template<typename T> std::string Pattern<T>::toString() const {
	return pattern->toString();
}

template<typename T, typename V> void SequencePattern<T,V>::set(const AutoPointer<TreeExpression::Expression>& expr, CopyPointer<Context>& context) const {
	a.set(expr, context);
	b.set(expr->getLeaf(), context);
}

template<typename T, typename V> int SequencePattern<T,V>::match(const AutoPointer<TreeExpression::Expression>& expr) {
	if (expr->hasLeaf()) {
		return a.match(expr) + b.match(expr->getLeaf());
	}
	else return 0;
}

template<typename T, typename V> void NodePattern<T,V>::set(const AutoPointer<TreeExpression::Expression>& expr, CopyPointer<Context>& context) const {
	node.set(expr, context);
	son.set(expr->getLeaf(), context);
}

template<typename T, typename V> int NodePattern<T,V>::match(const AutoPointer<TreeExpression::Expression>& expr) {
	if (expr->hasSon()) {
		int ris = node.match(expr);
		if (ris != 0) {
			int ris2 = son.match(expr->getSon());
			if (ris2 != 0) return 1+ris*ris2;
			else return 0;
		}
		else return 0;
	}
	else return 0;
}

}

#endif

